package com.tju.welding.dao;

import java.util.List;

import com.tju.welding.pojo.Project;
import com.tju.welding.pojo.ProjectMember;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

@Component
@Mapper
public interface ProjectMapper {

    @Insert("INSERT INTO project (project_name, creator_id, is_deleted) VALUES(#{project_name}, #{creator_id}, 0)")
    int addProject(@Param("project_name") String projectName, @Param("creator_id") int creatorId);

    @Insert("INSERT INTO project_member (project_id, member_id, member_type, is_deleted) VALUES(#{project_id}, #{member_id}, #{member_type}, 0)")
    int addProjectMember(@Param("project_id") int projectId, @Param("member_id") int memberId, @Param("member_type") int memberType);

    @Update("UPDATE project SET project_name = #{project_name} WHERE project_id = #{project_id}")
    int alterProjectName(@Param("project_id") int projectId, @Param("project_name") String projectName);

    @Select("SELECT * FROM project_member WHERE member_id = #{member_id}")
    List<ProjectMember> getProjectMemberByMemberId(@Param("member_id") int memberId);

    @Select("SELECT * FROM project_member WHERE member_id = #{member_id} AND project_id = #{project_id}")
    ProjectMember getProjectMemberByMemberIdAndProjectId(@Param("member_id") int memberId, @Param("project_id") int project_id);

    @Select("SELECT * FROM project WHERE project_id = #{project_id}")
    Project getProjectByProjectId(@Param("project_id") int projectId);

    @Select("SELECT * FROM project WHERE project_name = #{project_name}")
    Project getProjectByProjectName(@Param("project_name") String projectName);

    @Select("SELECT project_id FROM project WHERE project_name = #{project_name}")
    int getProjectIdByProjectName(@Param("project_name") String projectName);
    
}
